/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.model;

/**
 * This class describes a Recipe used by Dwarf to craft Item. All MMORecipeList are made of MMORecipeInstance (1 line of the recipe : Item-Quantity needed).
 */
public class MMORecipeList
{
	/** The table containing all MMORecipeInstance (1 line of the recipe : Item-Quantity needed) of the MMORecipeList */
	private MMORecipeInstance[] _recipes;
	
	/** The table containing all MMORecipeStatInstance for the statUse parameter of the MMORecipeList */
	private MMORecipeStatInstance[] _statUse;
	
	/** The table containing all MMORecipeStatInstance for the altStatChange parameter of the MMORecipeList */
	private MMORecipeStatInstance[] _altStatChange;
	
	/** The Identifier of the Instance */
	private final int _id;
	
	/** The crafting level needed to use this MMORecipeList */
	private final int _level;
	
	/** The Identifier of the MMORecipeList */
	private final int _recipeId;
	
	/** The name of the MMORecipeList */
	private final String _recipeName;
	
	/** The crafting success rate when using the MMORecipeList */
	private final int _successRate;
	
	/** The Identifier of the Item crafted with this MMORecipeList */
	private final int _itemId;
	
	/** The quantity of Item crafted when using this MMORecipeList */
	private final int _count;
	
	/** The Identifier of the Rare Item crafted with this MMORecipeList */
	private int _rareItemId;
	
	/** The quantity of Rare Item crafted when using this MMORecipeList */
	private int _rareCount;
	
	/** The chance of Rare Item crafted when using this MMORecipeList */
	private int _rarity;
	
	/** If this a common or a dwarven recipe */
	private final boolean _isDwarvenRecipe;
	
	/**
	 * Constructor of MMORecipeList (create a new Recipe).
	 * @param set
	 * @param haveRare
	 */
	public MMORecipeList(StatsSet set, boolean haveRare)
	{
		_recipes = new MMORecipeInstance[0];
		_statUse = new MMORecipeStatInstance[0];
		_altStatChange = new MMORecipeStatInstance[0];
		_id = set.getInteger("id");
		_level = set.getInteger("craftLevel");
		_recipeId = set.getInteger("recipeId");
		_recipeName = set.getString("recipeName");
		_successRate = set.getInteger("successRate");
		_itemId = set.getInteger("itemId");
		_count = set.getInteger("count");
		if (haveRare)
		{
			_rareItemId = set.getInteger("rareItemId");
			_rareCount = set.getInteger("rareCount");
			_rarity = set.getInteger("rarity");
		}
		_isDwarvenRecipe = set.getBool("isDwarvenRecipe");
	}
	
	/**
	 * Add a MMORecipeInstance to the MMORecipeList (add a line Item-Quantity needed to the Recipe).
	 * @param recipe
	 */
	public void addRecipe(MMORecipeInstance recipe)
	{
		int len = _recipes.length;
		MMORecipeInstance[] tmp = new MMORecipeInstance[len + 1];
		System.arraycopy(_recipes, 0, tmp, 0, len);
		tmp[len] = recipe;
		_recipes = tmp;
	}
	
	/**
	 * Add a MMORecipeStatInstance of the statUse parameter to the MMORecipeList.
	 * @param statUse
	 */
	public void addStatUse(MMORecipeStatInstance statUse)
	{
		int len = _statUse.length;
		MMORecipeStatInstance[] tmp = new MMORecipeStatInstance[len + 1];
		System.arraycopy(_statUse, 0, tmp, 0, len);
		tmp[len] = statUse;
		_statUse = tmp;
	}
	
	/**
	 * Add a MMORecipeStatInstance of the altStatChange parameter to the MMORecipeList.
	 * @param statChange
	 */
	public void addAltStatChange(MMORecipeStatInstance statChange)
	{
		int len = _altStatChange.length;
		MMORecipeStatInstance[] tmp = new MMORecipeStatInstance[len + 1];
		System.arraycopy(_altStatChange, 0, tmp, 0, len);
		tmp[len] = statChange;
		_altStatChange = tmp;
	}
	
	/**
	 * @return the Identifier of the Instance.
	 */
	public int getId()
	{
		return _id;
	}
	
	/**
	 * @return the crafting level needed to use this MMORecipeList.
	 */
	public int getLevel()
	{
		return _level;
	}
	
	/**
	 * @return the Identifier of the MMORecipeList.
	 */
	public int getRecipeId()
	{
		return _recipeId;
	}
	
	/**
	 * @return the name of the MMORecipeList.
	 */
	public String getRecipeName()
	{
		return _recipeName;
	}
	
	/**
	 * @return the crafting success rate when using the MMORecipeList.
	 */
	public int getSuccessRate()
	{
		return _successRate;
	}
	
	/**
	 * @return true if the Item crafted with this MMORecipeList is consumable (shot, arrow,...).
	 */
	public boolean isConsumable()
	{
		return (((_itemId >= 1463) && (_itemId <= 1467)) // Soulshots
			|| ((_itemId >= 2509) && (_itemId <= 2514)) // Spiritshots
			|| ((_itemId >= 3947) && (_itemId <= 3952)) // Blessed Spiritshots
		|| ((_itemId >= 1341) && (_itemId <= 1345)) // Arrows
		);
	}
	
	/**
	 * @return the Identifier of the Item crafted with this MMORecipeList.
	 */
	public int getItemId()
	{
		return _itemId;
	}
	
	/**
	 * @return the quantity of Item crafted when using this MMORecipeList.
	 */
	public int getCount()
	{
		return _count;
	}
	
	/**
	 * @return the Identifier of the Rare Item crafted with this MMORecipeList.
	 */
	public int getRareItemId()
	{
		return _rareItemId;
	}
	
	/**
	 * @return the quantity of Rare Item crafted when using this MMORecipeList.
	 */
	public int getRareCount()
	{
		return _rareCount;
	}
	
	/**
	 * @return the chance of Rare Item crafted when using this MMORecipeList.
	 */
	public int getRarity()
	{
		return _rarity;
	}
	
	/**
	 * @return {@code true} if this a Dwarven recipe or {@code false} if its a Common recipe
	 */
	public boolean isDwarvenRecipe()
	{
		return _isDwarvenRecipe;
	}
	
	/**
	 * @return the table containing all MMORecipeInstance (1 line of the recipe : Item-Quantity needed) of the MMORecipeList.
	 */
	public MMORecipeInstance[] getRecipes()
	{
		return _recipes;
	}
	
	/**
	 * @return the table containing all MMORecipeStatInstance of the statUse parameter of the MMORecipeList.
	 */
	public MMORecipeStatInstance[] getStatUse()
	{
		return _statUse;
	}
	
	/**
	 * @return the table containing all MMORecipeStatInstance of the AltStatChange parameter of the MMORecipeList.
	 */
	public MMORecipeStatInstance[] getAltStatChange()
	{
		return _altStatChange;
	}
}
